package ws.very.util.dingtalk.api

import com.dingtalk.open.client.ServiceFactory
import com.dingtalk.open.client.api.service.isv.IsvService
import com.dingtalk.open.client.api.service.isv.IsvServiceExtend
import scala.concurrent.{ExecutionContext, Future}

protected[dingtalk] object IsvServiceHelper {

  private val serviceFactory = ServiceFactory.getInstance
  private val isvService: IsvService = serviceFactory.getOpenService(classOf[IsvService])

  //val isvServiceExt: IsvServiceExtend = serviceFactory.getOpenService(classOf[IsvServiceExtend])

  def getSuiteToken(suiteKey: S, suiteSecret: S, suiteTicket: S)(implicit executor: ExecutionContext) = {
    Future {
      isvService.getSuiteToken(suiteKey, suiteSecret, suiteTicket).getSuite_access_token
    }
  }

  def getPermanentCode(suitToken: S, authCode: S)(implicit executor: ExecutionContext) = {
    Future {
      isvService.getPermanentCode(suitToken, authCode)
    }

  }

  def activateSuite(suiteToken: S, suiteKey: S, corpId: S)(implicit executor: ExecutionContext) = Future {
    isvService.activateSuite(suiteToken, suiteKey, corpId)
  }

  //(@ParamAttr(location = Location.URL,paramKey = "suite_access_token") String var1, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "auth_corpid") String var2, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "permanent_code") String var3)
  def getCorpToken(suiteToken: S, corpId: S, permanentCode: S)(implicit executor: ExecutionContext) = Future {
    val expiredTime = now.mill + 7200000
    val corpAuthToken = isvService.getCorpToken(suiteToken, corpId, permanentCode)
    (Option(corpAuthToken.getAccess_token), expiredTime)
  }

  //@ParamAttr(location = Location.URL,paramKey = "suite_access_token") String var1, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "suite_key") String var2, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "auth_corpid") String var3, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "permanent_code") String var4
  def getAuthInfo(suiteToken: S, suiteKey: S, corpId: S, permanentCode: S)(implicit executor: ExecutionContext) = Future {
    isvService.getAuthInfo(suiteToken, suiteKey, corpId, permanentCode)
  }

  //@ParamAttr(location = Location.URL,paramKey = "suite_access_token") String var1, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "suite_key") String var2, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "auth_corpid") String var3, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "agentid") String var4)
  def getAgent(suiteToken: S, suiteKey: S, corpId: S, agentId: S)(implicit executor: ExecutionContext) = Future {
    isvService.getAgent(suiteToken, suiteKey, corpId, agentId)
  }

  //  def getUnactiveCorps(accessToken: S, appId: S)(implicit executor: ExecutionContext) = Future {
  //    isvServiceExt.getUnactiveCorps(accessToken, appId)
  //  }
}
